package com.chen.controller;

import com.chen.entity.Book;
import com.chen.service.BookService;
import com.chen.vo.ResultVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class BookController {

    @Autowired
    BookService bookService;

    @GetMapping("/getAll")
    public String getAll(Model model) {
        List<Book> books = bookService.selectAll();
        model.addAttribute("books", books);
        return "booklist";
    }


    @GetMapping("/getPage")
    public String page(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum, Model model) {
        PageInfo<Book> page = bookService.getPage(pageNum);
        model.addAttribute("page", page);
        return "pagebook";
    }

    @PostMapping("/del")
    public String del(Integer[] ids) {
        if (ids.length > 0) {
            bookService.del(ids);
        }
        return "redirect:/getPage";
    }
}
